package com.etc.controller;

import com.etc.pojo.EmpMst;
import com.etc.pojo.Result;
import com.etc.service.AService;
import com.etc.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

// 语义Component
@Controller
public class AController {

    // Autowired是bytype,  +Qulifier 是byname  , @Resource Byname
//    @Autowired
//    @Qualifier("myService")
    @Resource(name="myService")
    IService aService;

    // SpringMVC
    // 接受aaa请求， 返回 json
    @RequestMapping(value="/aaa", method = RequestMethod.GET)
    @ResponseBody
    public List<HashMap> show() {
        // 接受请求返回结果
        System.out.println("AController.show()");
        List<HashMap> lh = aService.aaaaaa();
        return lh;
    }

    @RequestMapping(value="/bbb", method = RequestMethod.GET)
    @ResponseBody
    public Result getByname(@RequestParam("name") String name) {


        EmpMst emp = aService.findByName(name);
        if (emp.getId() == 0) {
            return Result.fail("数据不存在");
        }
        return Result.ok(emp);
    }

    @RequestMapping(value="/add", method = RequestMethod.POST)
    @ResponseBody
    public Result getByname(@RequestBody EmpMst emp) {


        int r = aService.add(emp);
        if (r != 1) {
            return Result.fail("插入失败");
        }
        return Result.ok("插入成功");
    }
}
